package com.sky.service.impl;

import com.sky.context.BaseContext;
import com.sky.dto.ShoppingCartDTO;
import com.sky.entity.Dish;
import com.sky.entity.Setmeal;
import com.sky.entity.ShoppingCart;
import com.sky.mapper.DishMapper;
import com.sky.mapper.SetmealMapper;
import com.sky.mapper.ShoppingCartMapper;
import com.sky.mapper.UserDishMapper;
import com.sky.service.ShoppingCartService;
import com.sky.vo.SetmealVO;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.web.bind.annotation.RequestBody;

import java.time.LocalDateTime;
import java.util.List;

@Service
@Slf4j
public class ShoppingCartServiceImpl implements ShoppingCartService {
    @Autowired
    DishMapper dishMapper;
    @Autowired
    SetmealMapper setmealMapper;
    @Autowired
    ShoppingCartMapper shoppingCartMapper;

    @Override
    public void addCart( ShoppingCartDTO shoppingCartDTO) {

        // 1.创建shoppingCart对象，拷贝属性值

        ShoppingCart shoppingCart=new ShoppingCart();
        BeanUtils.copyProperties(shoppingCartDTO,shoppingCart);
        shoppingCart.setCreateTime(LocalDateTime.now());
        // 2.判断该商品是否已经存在于购物车 条件：dishId&dishFlavor&userId
        //只查当前用户的购物车
        shoppingCart.setUserId(BaseContext.getCurrentId());
        //判断购物车中是否存在当前数据
        ShoppingCart cart=shoppingCartMapper.selectOne(shoppingCart);
        if(cart==null){
            ///如果购物车中没有该商品数据
            //判断是新增套餐还是新增菜品
            //新增套餐
            if (shoppingCartDTO.getSetmealId()!=null){
                SetmealVO setmealVo = setmealMapper.getSetmealConcreateById(shoppingCartDTO.getSetmealId());
                shoppingCart.setName(setmealVo.getName());
                shoppingCart.setAmount(setmealVo.getPrice());
                shoppingCart.setImage(setmealVo.getImage());
                shoppingCart.setNumber(1);

            }else { //新增菜品
                Dish dish = dishMapper.selectById(shoppingCartDTO.getDishId());
                shoppingCart.setName(dish.getName());
                shoppingCart.setAmount(dish.getPrice());
                shoppingCart.setImage(dish.getImage());
                shoppingCart.setNumber(1);


            }
            shoppingCartMapper.insert(shoppingCart);
        }
        else{
            //如果购物车中存在该商品数据，则更新数量
            //将原来购物车中的商品数量加一，调用mapper更新
            cart.setNumber(cart.getNumber()+1);
            shoppingCartMapper.update(cart);

        }



    }

    @Override
    public List<ShoppingCart> list() {
        return shoppingCartMapper.list(BaseContext.getCurrentId());
    }

    @Override
    public void clean() {
        shoppingCartMapper.clean(BaseContext.getCurrentId());
    }

    @Override
    public void subCart(ShoppingCartDTO shoppingCartDTO) {
        ShoppingCart shoppingCart=new ShoppingCart();
        BeanUtils.copyProperties(shoppingCartDTO,shoppingCart);
        shoppingCart.setUserId(BaseContext.getCurrentId());
        ShoppingCart cart=shoppingCartMapper.selectOne(shoppingCart);
       //获得当前购物车中商品信息，判断数量是否为最后一个
        if(cart.getNumber()==1){
            shoppingCartMapper.delete(cart.getId());
        }else {
            cart.setNumber(cart.getNumber()-1);
            shoppingCartMapper.update(cart);
        }
    }
}